class tileserArcgisHisImageryProvider extends Cesium.UrlTemplateImageryProvider {
    constructor(options) {
        // 设置默认的 minimumLevel 和 tilingScheme
        options.url = "https://wayback.maptiles.arcgis.com/";
        options.minimumLevel = 1;
        options.maximumLevel = 18;

        super(options);

        // 初始化 indexTime 属性
        this.indexTimeID = options.indexTimeID || 0; // 默认值为 1
    }

    async requestImage(x, y, level, request) {
        try {
            let imageUrl;
            if (this.indexTime !== 0) {
                imageUrl = this.buildImageUrl(this.indexTimeID, x, y, level);
            } else {
                imageUrl = `https://wayback.maptiles.arcgis.com/arcgis/rest/services/World_Imagery/WMTS/1.0.0/default028mm/MapServer/tile/${level}/${y}/${x}`;
            }
            return Cesium.ImageryProvider.loadImage(this, imageUrl);
        } catch (error) {
            return undefined;
        }
    }

    buildImageUrl(indexTimeID, x, y, level) {
        // 构建并返回基于时间信息的图像 URL
        // 这里的实现取决于你的 URL 结构和如何使用时间信息

        return `https://wayback.maptiles.arcgis.com/arcgis/rest/services/World_Imagery/WMTS/1.0.0/default028mm/MapServer/tile/${indexTimeID}/${level}/${y}/${x}`;
    }
}

async function arcgisXyzToAllInfo(x, y, level) {
    const response = await fetch(`http://tileser.giiiis.com/arcgis/${level}/${x}/${y}`);
    const jsonData = response.json();
    return jsonData;
}

async function getCurrentTileCoordinates(viewer) {
    const scene = viewer.scene;
    const ellipsoid = scene.globe.ellipsoid;
    const camera = scene.camera;

    // 获取相机的经纬度
    const cameraPositionCartographic = ellipsoid.cartesianToCartographic(camera.position);
    const longitude = Cesium.Math.toDegrees(cameraPositionCartographic.longitude);
    const latitude = Cesium.Math.toDegrees(cameraPositionCartographic.latitude);

    // 计算缩放级别 (Z)
    const cameraHeight = cameraPositionCartographic.height;
    const level = altToZoom(cameraHeight);

    // 转换经纬度为瓦片坐标 (X, Y)
    let x = Math.floor((longitude + 180) / 360 * Math.pow(2, level));

    let y = Math.floor((1 - Math.log(Math.tan(latitude * Math.PI / 180) + 1 / Math.cos(latitude * Math.PI / 180)) / Math.PI) / 2 * Math.pow(2, level));

    const allTimes = await arcgisXyzToAllInfo(x, y, level);

    return allTimes;
}



//3D级数转为2D高
function altToZoom(cameraHeight) {
    const levels = [
        { maxAlt: 250000000, level: 0 },
        { maxAlt: 25000000, level: 1 },
        { maxAlt: 9000000, level: 2 },
        { maxAlt: 7000000, level: 3 },
        { maxAlt: 4400000, level: 4 },
        { maxAlt: 2000000, level: 5 },
        { maxAlt: 1000000, level: 6 },
        { maxAlt: 493977, level: 7 },
        { maxAlt: 218047, level: 8 },
        { maxAlt: 124961, level: 9 },
        { maxAlt: 56110, level: 10 },
        { maxAlt: 40000, level: 11 },
        { maxAlt: 13222, level: 12 },
        { maxAlt: 7000, level: 13 },
        { maxAlt: 4000, level: 14 },
        { maxAlt: 2500, level: 15 },
        { maxAlt: 1500, level: 16 },
        { maxAlt: 600, level: 17 },
        { maxAlt: 250, level: 18 },
        { maxAlt: 150, level: 19 },
        { maxAlt: 50, level: 20 }
    ];

    for (const { maxAlt, level } of levels) {
        if (cameraHeight >= maxAlt) {
            return level + 1;
        }
    }

    return 20; // 默认级别
}
// 显示下拉菜单的函数
function displayDatesInDropdown(allTimes) {
    const dropdown = document.getElementById('dateDropdown');
    dropdown.innerHTML = ''; // 清空现有选项

    let closestIndex = -1;
    let closestDiff = Infinity;

    // 函数：计算两个日期字符串之间的天数差异
    function getDaysDifference(dateStr1, dateStr2) {
        const date1 = new Date(dateStr1);
        const date2 = new Date(dateStr2);
        const diffTime = Math.abs(date2 - date1);
        return diffTime / (1000 * 60 * 60 * 24);
    }

    allTimes.time.forEach((time, index) => {
        const option = document.createElement('option');
        option.value = time;
        option.textContent = time;
        dropdown.appendChild(option);

        // 寻找与 currentSelectedTime 最接近的时间
        const diff = getDaysDifference(time, currentSelectedTime);
        if (diff < closestDiff) {
            closestDiff = diff;
            closestIndex = index;
        }
    });

    // 如果找到最接近的时间，则选择它
    if (closestIndex !== -1) {
        dropdown.selectedIndex = closestIndex;// 更新当前选中的时间
    }

    if (allTimes.time.length > 0) {
        document.getElementById('dateDropdownContainer').style.display = 'block';
    } else {
        document.getElementById('dateDropdownContainer').style.display = 'none';
    }

    dropdown.onchange = function () {
        currentSelectedTime = dropdown.value; // 更新当前选中的时间
        let timeID = findTimeID(allTimes, currentSelectedTime);
        updateImageryLayer(timeID);
    };
}

//根据时间数组，找到时间ID
function findTimeID(allTimes, time) {
    const timeIdMap = allTimes.time.reduce((map, time, index) => {
        map[time] = allTimes.timeID[index];
        return map;
    }, {});

    const selectedId = timeIdMap[time]; // 获取对应的 ID
    return selectedId;
}
